
//JumperSprite.java
//Andrew Davison, April 2005, ad@fivedots.coe.psu.ac.th

/* A sprite can move left/right, jump and stand still.
   In fact, a sprite doesn't move horizontally at all, so
   the left and right movement requests only change various
   status flags, not its locx value.

   The sprite has looping images for when it is moving
   left or right, and single images for when it is
   standing still or jumping.

   The sprite stores its world coordinate in (xWorld, yWorld).

   Jumping has a rising and falling component. Rising and 
   falling can be stopped by the sprite hitting a brick.

   The sprite's movement left or right can be stopped by hitting 
   a brick.

   A sprite will start falling if it walks off a brick into space.

   Brick queries (mostly about collision detection) are sent
   to the BricksManager object.
 */

import java.awt.*;


public class ShipSprite extends Sprite
{

	// total time to cycle through all the images

	// used by vertMoveMode 
	//  (in J2SE 1.5 we could use a enumeration for these)


	// max number of steps to take when rising upwards in a jump


	private boolean isFacingRight, isStill, movingUp, movingDown;

	private BricksManager brickMan;
	private RibbonsManager ribbMan;
	private int moveSize;   // obtained from BricksManager

	private int xWorld, yWorld;
	public MissileSprite missle = null;



	private ImagesLoader imagesLd; 
	private boolean loadMissle = true;
	/* the current position of the sprite in 'world' coordinates.
       The x-values may be negative. The y-values will be between
       0 and pHeight. */ 


	public ShipSprite(int w, int h, int brickMvSz, BricksManager bm,RibbonsManager rm, 
			ImagesLoader imsLd, int p)
	{
		super(w/2, h/2, w, h, imsLd, "ShipRight");
		imagesLd = imsLd;
		// standing center screen, facing right
		moveSize = brickMvSz;
		// the move size is the same as the bricks ribbon

		brickMan = bm;
		ribbMan = rm;
		setStep(0,0);     // no movement

		isFacingRight = true;
		isStill = true;
		movingUp = false;
		movingDown = false;

		/* Adjust the sprite's y- position so it is
       standing on the brick at its mid x- psoition. */
		//locy = brickMan.findFloor(locx+getWidth()/2)-getHeight();
		xWorld = locx; 
		yWorld = locy;    // store current position

		//vertMoveMode = NOT_JUMPING;
		//vertStep = brickMan.getBrickHeight()/2;   
		// the jump step is half a brick's height

	}  // end of JumperSprite()


	public void moveUp(){
		movingUp = true;
	}

	public void moveDown(){
		movingUp = false;
	}

	public void moveLeft()
	/* Request that the sprite move to the left. It doesn't
     actually move, but changes its image and status flags. */
	{ //setImage("ShipLeft");
	//loopImage(period, DURATION);   // cycle through the images
	isFacingRight = false;  
	isStill = false;
	}

	public void moveRight()
	/* Request that the sprite move to the right. It doesn't
     actually move, but changes its image and status flags. */
	{ //setImage("ShipRight");
	//loopImage(period, DURATION);   // cycle through the images
	isFacingRight = true;  
	isStill = false;
	}

	public void stayStill()
	/* Request that the sprite stops. It stops the
     image animation and sets the isStill status flag. */
	{ 
		//stopLooping(); 
		isStill = true;
	}

	public void missileUpdate(){
		missle.updateSprite();	
	}

	public boolean shootMissle(){

		if (loadMissle){
			if (isFacingRight){
				shootMissleRight();		
			}else{
				shootMissleRight();				
			}
		}

		return true;
	}

	private void shootMissleRight(){
		if (missle == null){
			missle	= new MissileSprite(locx+1,locy-8,imagesLd,"missleRight",45);
		}else if (missle.loadMissle){
			missle	= new MissileSprite(locx+1,locy-8,imagesLd,"missleRight",45);
		}

	}

	private void shootMissleLeft(){
		if (missle == null){
			missle	= new MissileSprite(locx+3,locy-8,imagesLd,"missleLeft",-45);	
		}else if(missle.loadMissle){
			missle	= new MissileSprite(locx+3,locy-8,imagesLd,"missleLeft",-45);
		}
		
	}

	public boolean willHitBrick()
	/* Test if the next x position is inside a brick
     Ignore any y motion. This method should always
     be called before updateSprite() makes the actual
     move. */
	{
		if (isStill)
			return false;   // can't hit anything if not moving

		int xTest;   // for testing the new x- position
		if (isFacingRight)   // moving right
			xTest = xWorld + moveSize;
		else // moving left
			xTest = xWorld - moveSize;

		// test a point near the base of the sprite
		int xMid = xTest + getWidth()/2;
		int yMid = yWorld + (int)(getHeight()*0.8);   // use current y posn

		return brickMan.insideBrick(xMid,yMid);  
	}  // end of willHitBrick()


	public void updateSprite(boolean shipMovingVert,boolean shipMovingHorz)
	/* Although the sprite is not moving in the x-direction, we 
     must still update its (xWorld, yWorld) coordinate. Also,
     if the sprite is jumping then its y position must be
     updated with moveVertically(). updateSprite() should
     only be called after collsion checking with willHitBrick()
	 */
	{
		//if (!isStill) {    // moving

		if (shipMovingHorz){
			if (isFacingRight){  // moving right
				locx += 4;
				//xWorld += moveSize;
			}else{
				// moving left
				locx -= 4;
				//xWorld -= moveSize;
			}

		}else{
			//ribbMan.stayStill();
			//brickMan.stayStill();
			//this.stayStill();
		}


		if (shipMovingVert){
			if (movingUp){
				locy-=moveSize*0.6;
			}else
				locy+=moveSize*0.6;
		}

		//}
		// vertical movement has two components: RISING and FALLING

		super.updateSprite();
	}  // end of updateSprite()

}  // end of JumperSprite

